import { dynamicCSS } from './dynamicCss.js';
import { verify } from './verify.js';
import { rules } from './rules.js';
// import { optionSide } from './optionSide.js';

dynamicCSS('/css/check.css');

var checkBoxItem = function (options, parent) {
    this.options = $.extend({}, {
        key: null,
        className: '',
        name: '',
        'default': false,
        readonly: false,
        description: null,
        onChange: function () { },
    }, options);

    if (!this.options.key) {
        console.error('checkBoxItem => create : 不存在的key值');
        return false;
    };

    var html = '';
    html += '<div class="form_checkBox_Item" ' + (options.description ? ('title="' + options.description + '"') : '') + '>';
    html += '   <span class="form_checkBox_choose"></span>';
    html += '   <div class="form_checkBox_name">' + options.name + '</div>';
    html += '</div>';
    this.parent = parent;
    this.html = $(html);
    this._element = this.html.find('.form_checkBox_choose');
    this.objectName = this.options.key;
    this.valueData = false;
    var _this = this;
    this.html.unbind().bind('click', function () {
        _this.valueData = !_this.valueData;
        _this.change();
        // _this.parent.options.changeEvent(_this, _this.parent);
        if (_this.parent.layout) {
            _this.parent.layout.hideError();
        };
    });
    this.event = {
        change: this.options.onChange
    };
    this.isReadonly = null;
    this.readonly(this.options.readonly);
    this.setDefaultValue();
};
checkBoxItem.prototype = {
    constructor: this,
    render: function () {
        return this.html;
    },
    // 绑定事件集合方法
    on: function (str, event) {
        if (this.event.hasOwnProperty(str)) {
            this.event[str] = event;
        };
        return this;
    },
    // 单独绑定 change 方法
    onChange: function (event) {
        this.event.change = event;
        return this;
    },
    change: function () {
        var _this = this;
        if (this.valueData) {
            this._element.addClass('active');
        } else {
            this._element.removeClass('active');
        };
        this.event.change.apply(this, [this.valueData, this.objectName]);
    },
    set: function (data) {
        if (data.toString().toLowerCase() == 'true') {
            this.valueData = true;
        } else if (data.toString().toLowerCase() == 'false') {
            this.valueData = false;
        };
        this.change();
        return this;
    },
    get: function (callBack) {
        if (this.parent.verify()) {
            callBack(false, this.valueData);
        } else {
            var errorText = '至少选择' + this.parent.options.checkLength.toString() + '项';
            callBack(errorText, this.valueData);
            if (this.parent.layout) {
                this.parent.layout.showError(errorText);
            };
        }
        return this;
    },
    readonly: function (isReadonly) {
        var _this = this;
        var _isReadonly = null;
        var funType = verify.is(isReadonly);


        if (funType == 'function') {
            _isReadonly = isReadonly();
        } else {
            _isReadonly = !!isReadonly;
        };
        this.html.attr('readonly', _isReadonly);
        if (_isReadonly) {
            this.isReadonly = true;
            this.html.addClass('readonly');
            this.html.unbind('click');
        } else {
            this.isReadonly = false;
            this.html.removeClass('readonly');
            this.html.unbind().bind('click', function () {
                _this.valueData = !_this.valueData;
                _this.change();
                if (_this.parent.layout) {
                    _this.parent.layout.hideError();
                }
            });
        };
        return this;
    },
    setDefaultValue: function () {
        this.set(this.options['default']);
        return this;
    }
};
var checkBox = function (options, layout, parentItems) {
    this.options = $.extend({}, {
        className: '',
        size: 'normal',
        checkLength: 0, // 必须选择的个数
        element: [],
    }, options);

    this.parent = parentItems;
    this.html = $('<div class="form_checkBox"></div>');
    this.layout = layout;
    this.errorText = null;
    this.options = $.extend({}, this.options, options);
    this.items = [];
    this.setData(this.options.element);
};
checkBox.prototype = {
    constructor: this,
    verify: function () {
        var _length = 0;
        if (_length == this.options.checkLength) {
            // 默认为0,不进行检查
            return true;
        } else {
            for (var key in this.items) {
                if (this.items.hasOwnProperty(key)) {
                    var element = this.items[key];
                    if (element.valueData) {
                        _length++;
                    }
                }
            };
            if (_length >= this.options.checkLength) {
                return true;
            } else {
                return false;
            };
        };
        return this;
    },
    get: function (callBack) {
        var data = {};
        var errorText = null;

        for (var key in this.items) {
            if (this.items.hasOwnProperty(key)) {
                var element = this.items[key];
                element.get(function (error, value) {
                    if (error) {
                        errorText = error;
                    } else {
                        data[element.options.key] = value;
                    }
                });
            }
        };
        if (errorText) {
            callBack(errorText);
        } else {
            callBack(false, data);
        };
        return this;
    },
    set: function (data) {
        for (var key in this.items) {
            if (data.hasOwnProperty(key)) {
                var element = this.items[key];
                element.set(data[key]);
            } else {
                data[key] = this.items[key].valueData;
            };
        };
        return this;
    },
    setData: function (data) {
        for (var i = 0; i < data.length; i++) {
            var checkBoxItemOptions = data[i];
            var checkBoxObject = new checkBoxItem(checkBoxItemOptions, this);
            if (this.parent && this.parent.hasOwnProperty(checkBoxItemOptions.key)) {
                console.warn('父级已经存在对象' + checkBoxItemOptions.key + ', 已经替换');
            };
            if (this.parent) {
                this.parent.item[checkBoxItemOptions.key] = checkBoxObject;
                this.parent.order.push(checkBoxItemOptions.key);
            };
            this.items[checkBoxItemOptions.key] = checkBoxObject;
            if (this.layout) {
                this.layout.append(checkBoxObject.render());
            } else {
                this.html.addClass(this.options.className);
                this.html.addClass(this.options.size);
                this.html.append(checkBoxObject.render());
            };
        };
        return this;
    },
};

export { checkBox };